package com.ykht.proj.common.security.util;


import com.ykht.proj.common.core.constant.CacheConstants;
import com.ykht.proj.common.security.service.ProjUser;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;

import org.springframework.cache.CacheManager;


/**
 * //修改security中用户信息.不可行
 * //Authentication authentication = SecurityUtils.getAuthentication();
 * //SecurityContextHolder.getContext().setAuthentication(new UsernamePasswordAuthenticationToken(newUser, authentication.getCredentials(), authentication.getAuthorities()));
 * 其他线程获取用户信息还是登陆时的信息，所以产生了此类 ，用于业务服务调用
 */
@Component
public class SecurityUtilsExt {

	@Resource
	private CacheManager tempCacheManager;
	private static CacheManager cacheManager;

	@PostConstruct
	public void init() {
		cacheManager = tempCacheManager;
	}

	public static Long getSjId() {
		String username = SecurityUtils.getUser().getUsername();
		Integer userType = SecurityUtils.getUser().getUserType();
		if (userType.equals(1) || userType.equals(2) || userType.equals(3) || userType.equals(5)) {
			ProjUser cacheUser = (ProjUser) cacheManager
					.getCache(CacheConstants.USER_DETAILS)
					.get(username)
					.get();
			return cacheUser.getSjId();
		}
		return null;
	}

}
